<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="close"
    :title="title"
    width="800px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item
            label="问题标题"
            prop="title"
            :rules="{ required: true, message: '问题标题不能为空', trigger: 'blur' }"
            style="width: 100%"
          >
            <el-input v-model="form.title" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="问题标签"
            prop="bq"
            :rules="{ required: true, message: '标签不能为空', trigger: 'blur' }"
          >
            <el-select
              v-model="bqList"
              multiple
              :multiple-limit="4"
              filterable
              allow-create
              default-first-option
              placeholder="请选择问题标签"
              style="width: 100%"
            >
              <el-option
                v-for="item in dict.consult_list"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="解答内容"
            prop="nr"
            :rules="{ required: true, message: '解答内容', trigger: 'blur' }"
            style="width: 100%"
            maxlength="200"
            show-word-limit
          >
            <el-input v-model="form.nr" type="textarea" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="是否常见问题"
            prop="cjwt"
            :rules="{ required: true, message: '请选则是否常见问题', trigger: 'change' }"
          >
            <el-radio-group v-model="form.cjwt">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="是否可见"
            prop="sfkj"
            :rules="{ required: true, message: '请选则是否可见', trigger: 'change' }"
          >
            <el-radio-group v-model="form.sfkj">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="是否已解答"
            prop="sfjd"
            :rules="{ required: true, message: '请选择是否已解答', trigger: 'change' }"
          >
            <el-radio-group v-model="form.sfjd">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { adminSaveConsult, adminUpdateConsult } from '@/api/consult/index'
export default {
  name: 'ConsultManagerEntity',
  dicts: ['consult_list'],
  data() {
    return {
      dialogFormVisible: false,
      loading: false,
      title: '新增',
      form: {
        id: null,
        title: '',
        bq: '',
        nr: '',
        cjwt: false,
        sfkj: false,
        sfjd: false,
        qz: 0,
        glwt: ''
      },
      bqList: [],
      status: [{ id: 0, value: false, label: '否' },
        { id: 1, value: true, label: '是' }]
    }
  },
  watch: {
    bqList: {
      handler(newVal, oldVal) {
        this.form.bq = newVal.toString()
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: true // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    openEntity(data) {
      this.bqList = []
      if (undefined === data || data === null || data.id === undefined || data.id === null) {
        this.title = '新增'
        this.dialogFormVisible = true
      } else {
        this.title = '编辑'
        if (undefined !== data.bq && data.bq !== null && data.bq !== '') {
          this.bqList = data.bq.split(',')
        }
        this.dialogFormVisible = true
      }
      this.form = Object.assign({}, data)
    },
    save(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.form.id !== null && undefined !== this.form.id) {
            adminUpdateConsult(this.form).then(res => {
              this.$message.success('更新成功！')
              this.close()
              this.loading = false
            }).catch(() => {
              this.$message.error('更新失败！')
              this.loading = false
            })
          } else {
            adminSaveConsult(this.form).then(res => {
              this.$message.success('保存成功！')
              this.loading = false
              this.close()
            }).catch(() => {
              this.loading = false
              this.$message.error('保存失败！')
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    close() {
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    }
  }
}
</script>

<style scoped>

</style>
